home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk16 / cat / cat.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  4KB  |  131 lines

  1. /*  Cat.c
  2.  
  3.     Takes two filenames as arguments and concatenates the first file
  4.     onto the end of the second file. Avoids the problems of "join"
  5.     not being able to copy into an existing file.
  6.  
  7.     Usage: cat [-a/-o/-s "string"] [file1 [,file2...]] [to] file
  8.  
  9.     written by: DM Brown
  10.     date:       19 April 1988
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <dos.h>
  15.  
  16. #define FAILED 1
  17.  
  18. char APPEND[] = "a",
  19.      WRITE[] = "w";
  20.  
  21. void main(argc, argv)
  22. int argc;
  23. char *argv[];
  24. {
  25.     int c, i, n;
  26.     FILE *fin, *fout = NULL;
  27.     char *mode = APPEND;
  28.     void printCliArgs(), errmsg();
  29.  
  30. /*  Check for minimum number of arguments. At minimum, one source file and
  31.     one destination file must be given.
  32. */
  33.     if (argc < 3)
  34.     {
  35.         printf("Usage: cat [-a/-o/-s \"string\"] [file1 [,file2...]] [to] file\n");
  36.         exit(FAILED);
  37.     }
  38.  
  39. /*  Check that the output file name does not begin with '-', which probably
  40.     means that no output file was given.
  41. */
  42.     if (argv[argc-1][0] == '-')
  43.     {
  44.         printf("Error in arguments.\n");
  45.         exit(FAILED);
  46.     }
  47.  
  48. /*  Test for key word 'to' and cause it to be ignored   */
  49.  
  50.     if ((c = tolower(argv[argc-2][0])) == 't' && 
  51.         (c = tolower(argv[argc-2][1])) == 'o')
  52.         n = 2;
  53.     else n = 1;
  54.  
  55. /*  Repeat for all the remaining arguments  */
  56.  
  57.     for (i = 1; i < argc - n; i++)
  58.     {
  59.         if (argv[i][0] == '-')
  60.             switch(tolower(argv[i][1]))
  61.             {
  62.                 case 's':
  63.  
  64. /*  Check that some text follows the '-s' option. Note that all quote
  65.     marks are stripped BEFORE being placed in the argument list. Therefore
  66.     cannot test for VALID text (ie - text following '-s' COULD be a file
  67.     spec or another argument. This text would be interpreted as plain
  68.     text and appended to the end of the file. This check avoids the problem
  69.     of having the output filename immediately follow the -s option.
  70. */
  71.                     if (++i < argc - n)
  72.                     {
  73.                         if (fout == NULL)
  74.                         /*  Open output file and exit on failure    */
  75.                             if ((fout = fopen(argv[argc-1], mode)) == NULL)
  76.                                 errmsg(argv[argc-1]);
  77.  
  78.                         fprintf(fout, "%s\n", argv[i]);
  79.                     }
  80.                     else
  81.                     {
  82.                         printf("Error in arguments.\n");
  83.                         exit(FAILED);
  84.                     }
  85.                     break;
  86.  
  87. /*  Note that once writing has begun, the following two options will be
  88.     read and handled, but will not have any effect.
  89. */
  90.                 case 'a':   /* set mode to append   */
  91.                     mode = APPEND;
  92.                     break;
  93.  
  94.                 case 'o':   /* set mode to overwrite    */
  95.                     mode = WRITE;
  96.                     break;
  97.             }
  98.         else
  99.         {
  100.             if ((fin = fopen(argv[i], "r")) == NULL)
  101.                 errmsg(argv[i]);
  102.  
  103.             if (fout == NULL)
  104.             /*  Open output file and exit on failure    */
  105.                 if ((fout = fopen(argv[argc-1], mode)) == NULL)
  106.                     errmsg(argv[argc-1]);
  107.  
  108.             while ((c = getc(fin)) != EOF)
  109.                 putc(c, fout);
  110.  
  111.             fclose(fin);
  112.         }
  113.     }
  114.  
  115. /*  check to see if output file was even opened */
  116.     if (fout == NULL)
  117.         printf("Nothing to do.\n");
  118.     else
  119.         fclose(fout);
  120. }
  121.  
  122. void errmsg(s)
  123. char *s;
  124. {
  125.     char eline[256];
  126.  
  127.     sprintf(eline,"Cannot open %s", s);
  128.     (void)poserr(eline);
  129.     exit(FAILED);
  130. }
  131.